﻿using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace SceneUnpacker.Marc.WindowStateMonitor
{
    public class WindowStateMonitor : ISupportInitialize
    {
        protected WeakReference form;
        protected bool hooked;
        protected string name;
        protected object tag;
        protected FormWindowState windowState;

        public event EventHandler Maximized;

        public event EventHandler Minimized;

        public event EventHandler Normalized;

        public event EventHandler Restored;

        public WindowStateMonitor()
        {
        }

        public WindowStateMonitor(System.Windows.Forms.Form form)
        {
            this.form = new WeakReference(form);
            this.Hook();
        }

        public void BeginInit()
        {
        }

        public void EndInit()
        {
            if ((this.form != null) && !this.hooked)
            {
                this.Hook();
            }
        }

        public void Hook()
        {
            this.windowState = ((System.Windows.Forms.Form) this.form.Target).WindowState;
            ((System.Windows.Forms.Form) this.form.Target).Layout += new LayoutEventHandler(this.OnLayout);
        }

        private void OnLayout(object sender, LayoutEventArgs e)
        {
            if (((System.Windows.Forms.Form) this.form.Target).WindowState != this.windowState)
            {
                switch (((System.Windows.Forms.Form) this.form.Target).WindowState)
                {
                    case FormWindowState.Normal:
                        if (this.Normalized != null)
                        {
                            this.Normalized(this, EventArgs.Empty);
                        }
                        if ((this.windowState == FormWindowState.Minimized) && (this.Restored != null))
                        {
                            this.Restored(this, EventArgs.Empty);
                        }
                        break;

                    case FormWindowState.Minimized:
                        if (this.Minimized != null)
                        {
                            this.Minimized(this, EventArgs.Empty);
                        }
                        break;

                    case FormWindowState.Maximized:
                        if (this.Maximized != null)
                        {
                            this.Maximized(this, EventArgs.Empty);
                        }
                        if ((this.windowState == FormWindowState.Minimized) && (this.Restored != null))
                        {
                            this.Restored(this, EventArgs.Empty);
                        }
                        break;
                }
                this.windowState = ((System.Windows.Forms.Form) this.form.Target).WindowState;
            }
        }

        public System.Windows.Forms.Form Form
        {
            get
            {
                return (System.Windows.Forms.Form) this.form.Target;
            }
            set
            {
                this.form = new WeakReference(value);
            }
        }

        public string Name
        {
            get
            {
                return this.name;
            }
            set
            {
                this.name = value;
            }
        }

        public object Tag
        {
            get
            {
                return this.tag;
            }
            set
            {
                this.tag = value;
            }
        }
    }
}

